package com.gxtbindings.showcase.client.demo;

import com.extjs.gxt.ui.client.data.BeanModelTag;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.Text;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.gxtbindings.bindings.client.BindingGroup;
import com.gxtbindings.bindings.client.CommitSet;
import com.gxtbindings.bindings.client.converters.IntegerConverter;

public class BindingGroupDemo extends LayoutContainer {

	private TextField<String> firstField;
	
	private TextField<String> lastField;
	
	private NumberField ageField;
	
	private TextField<String> dateOfBirthField;
	
	private TextField<String> anotherFirstField;
	
	private TextField<String> anotherLastField;
	
	private NumberField anotherAgeField;
	
	private Button commitButton;
	
	private Button rejectButton;
	
	private CommitSet commitSet;
	
	private SomeDto someDto;
	
	private Person person;
	
	public BindingGroupDemo() {
		
		initComponents();
		doBinding();
		layoutContainer();
	}
	
	private void initComponents()
	{
		firstField = new TextField<String>();
		lastField = new TextField<String>();
		ageField = new NumberField();
		dateOfBirthField = new TextField<String>();
		anotherFirstField = new TextField<String>();
		anotherLastField = new TextField<String>();
		anotherAgeField = new NumberField();
		
		commitButton = new Button("Commit", new SelectionListener<ButtonEvent>() {
			public void componentSelected(ButtonEvent ce) {
				commitSet.commitChanges();
			}
		});
		
		rejectButton = new Button("Reject", new SelectionListener<ButtonEvent>() {
			public void componentSelected(ButtonEvent ce) {
				commitSet.rejectChanges();
			}
		});
	}
	
	private void layoutContainer() {

		// First binding
		TableLayout t = new TableLayout(4);
		t.setCellPadding(10);
		LayoutContainer c1 = new LayoutContainer(t);
		c1.setStyleAttribute("backgroundColor", "#ddaaaa");
		c1.add(new Text("First/Last/Age:"));
		c1.add(firstField);
		c1.add(lastField);
		c1.add(ageField);
		c1.add(new Text("Date of birth:"));
		c1.add(dateOfBirthField);
		
		// Second binding
		TableLayout t2 = new TableLayout(4);
		t2.setCellPadding(10);
		LayoutContainer c2 = new LayoutContainer(t2);
		c2.setStyleAttribute("backgroundColor", "#aaaadd");
		c2.add(new Text("First/Last/Age:"));
		c2.add(anotherFirstField);
		c2.add(anotherLastField);
		c2.add(anotherAgeField);

		setLayout(new TableLayout(1));
		add(c1);
		add(c2);
		
		add(commitButton);
		add(rejectButton);
	}
	
	private void doBinding()
	{
		// Init test objs
		someDto = new SomeDto();
		someDto.setPerson(new Person("John", "Smith", 18));
		someDto.setDateOfBirth("10/10/2010");
		
		person = new Person("Erica", "Robinson", 24);
		
		BindingGroup someDtoBinding = new BindingGroup();
		someDtoBinding.add(firstField, "person.firstName");
		someDtoBinding.add(lastField, "person.lastName");
		someDtoBinding.add(ageField, "person.age", new IntegerConverter());
		someDtoBinding.add(dateOfBirthField, "dateOfBirth");
		someDtoBinding.bind(someDto);
		
		BindingGroup personBinding = new BindingGroup();
		personBinding.add(anotherFirstField, "firstName");
		personBinding.add(anotherLastField, "lastName");
		personBinding.add(anotherAgeField, "age", new IntegerConverter());
		personBinding.bind(person);
		
		commitSet = new CommitSet();
		commitSet.add(someDtoBinding);
		commitSet.add(personBinding);
	}
	
	public class SomeDto implements BeanModelTag
	{
		private Person person;
		
		private String dateOfBirth;
		
		public String getDateOfBirth() {
			return dateOfBirth;
		}

		public void setDateOfBirth(String dateOfBirth) {
			this.dateOfBirth = dateOfBirth;
		}

		public Person getPerson() {
			return person;
		}

		public void setPerson(Person person1) {
			this.person = person1;
		}
	}
}
